/*
 * Copyright (c) 2010, 2011
 *	Open Source Technologies Ltd.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the company nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COMPANY AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>

#include "defs.h"
#include "string.h"

static int string_adjust_buffer __p((string_t *str, size_t len));

static const size_t string_min_capacity = 16;

string_t *
string_create(void)
{
	string_t *str;

	str = (string_t *)malloc(sizeof(string_t));
	if (str == NULL)
		return NULL;

	str->s_magic = STRING_DEAD;
	string_initialize(str);

	return str;
}

void
string_initialize(string_t *str)
{
	assert(is_string_dead(str));

	memset((void *)str, 0, sizeof(string_t));
	str->s_magic = STRING_MAGIC;
}

#ifndef NDEBUG

size_t
string_length(const string_t *str)
{
	assert(is_string(str));

	return str->s_length;
}

size_t
string_capacity(const string_t *str)
{
	assert(is_string(str));

	return str->s_capacity;
}

char *
string_cstr(const string_t *str)
{
	assert(is_string(str));

	return str->s_buffer ? str->s_buffer : "";
}

#endif /* !NDEBUG */

string_t *
string_append(string_t *dest, const string_t *src)
{
	size_t new_len;

	assert(is_string(dest));
	assert(is_string(src));

	new_len = dest->s_length + src->s_length;
	if (string_adjust_buffer(dest, new_len) < 0)
		return NULL;

	memcpy(dest->s_buffer + dest->s_length, src->s_buffer, src->s_length + 1);
	dest->s_length = new_len;

	return dest;
}

string_t *
string_append_mem(string_t *dest, const char *src, size_t len)
{
	size_t new_len;

	assert(is_string(dest));
	assert(src != NULL);

	new_len = dest->s_length + len;
	if (string_adjust_buffer(dest, new_len) < 0)
		return (string_t *)NULL;

	memcpy(dest->s_buffer + dest->s_length, src, len);

	dest->s_buffer[new_len] = 0;
	dest->s_length          = new_len;

	return dest;
}

string_t *
string_append_cstr(string_t *dest, const char *src)
{
	size_t src_len;
	size_t new_len;

	assert(is_string(dest));
	assert(src != NULL);

	src_len = strlen(src);
	new_len = dest->s_length + src_len;
	if (string_adjust_buffer(dest, new_len) < 0)
		return NULL;

	memcpy(dest->s_buffer + dest->s_length, src, src_len + 1);
	dest->s_length = new_len;

	return dest;
}

string_t *
string_append_char(string_t *dest, char src)
{
	assert(is_string(dest));
	assert(src != 0);

	if (string_adjust_buffer(dest, dest->s_length + 1) < 0)
		return NULL;

	dest->s_buffer[dest->s_length++] = src;
	dest->s_buffer[dest->s_length]   = 0;

	return dest;
}

int
string_compare(const string_t *left, const string_t *right)
{
	assert(is_string(left));
	assert(is_string(right));

	return memcmp(
		string_cstr(left),
		string_cstr(right),
		min(string_length(left), string_length(right))
	);
}

int
string_compare_mem(const string_t *left, const char *right, size_t len)
{
	assert(is_string(left));
	assert(right != NULL);

	return memcmp(
		string_cstr(left),
		right,
		min(string_length(left), len)
	);
}

int
string_compare_cstr(const string_t *left, const char *right)
{
	assert(is_string(left));
	assert(right != NULL);

	return strncmp(
		string_cstr(left),
		right,
		string_length(left)
	);
}

void
string_finalize(string_t *str)
{
	assert(is_string(str));

	if (str->s_buffer != NULL)
		free(str->s_buffer);

	str->s_magic = STRING_DEAD;
}

string_t *
string_destroy(string_t *str)
{
	assert(is_string(str));

	string_finalize(str);
	free(str);

	return NULL;
}

static int
string_adjust_buffer(string_t *str, size_t len)
{
	char   *buffer;
	size_t  capacity;

	assert(is_string(str));

	if (len < str->s_capacity) {
		if (len > str->s_capacity / 2)
			return 0;
		if (str->s_capacity && (str->s_capacity <= string_min_capacity))
			return 0;
	}

	capacity = len * 2 + 1;
	if (capacity < string_min_capacity)
		capacity = string_min_capacity;

	buffer = (char *)realloc(str->s_buffer, capacity);
	if (buffer == NULL)
		return -1;

	str->s_buffer   = buffer;
	str->s_capacity = capacity;

	return 0;
}
